// $Id$
// Licensed under Apache License Version 2.0
// See: http://www.apache.org/licenses/LICENSE-2.0.html
package de.servicezombie.tcmdb.dao.mongo;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public abstract class AbstractMongoObject implements MongoDomainObject {

	@Override
	public int hashCode() {
		return new HashCodeBuilder()
			.append(getId())
			.append(getRevision()).toHashCode();
	}

	@Override
	public boolean equals(Object obj) {
		
		/* not 100% safe, but we treat objects with same id as equal */		
		if (obj != null && obj instanceof MongoDomainObject) {
			MongoDomainObject rhs = (MongoDomainObject) obj;
			return new EqualsBuilder()
					.append(getId(), rhs.getId())
					.append(getRevision(), rhs.getRevision())
					.isEquals();
		}
		else {
			return false;
		}
	}
	
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
	}
}
